package com.snow.useviewdemo.function.wallpaper;

import android.Manifest;
import android.app.Activity;
import android.app.WallpaperInfo;
import android.app.WallpaperManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import androidx.core.content.ContextCompat;


import com.snow.baselibrary.utils.PhoneRomUtil;

import java.io.IOException;

public class SZWallpaperUtils {
    /**
     * 跳转到系统设置壁纸界面
     *
     * @param context
     */
    public static void setLiveWallpaper(Context context, int requestCode) {
        try {
            Intent localIntent = new Intent();
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {// ICE_CREAM_SANDWICH_MR1  15
                localIntent.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);// android.service.wallpaper.CHANGE_LIVE_WALLPAPER
                // android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT
                ComponentName componentName = new ComponentName(context.getApplicationContext().getPackageName(),
                        SZWallpaperService.class.getCanonicalName());
                localIntent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, componentName);
            } else {
                localIntent.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);// android.service.wallpaper.LIVE_WALLPAPER_CHOOSER
            }
            ((Activity) context).startActivityForResult(localIntent, requestCode);
//            Toast.makeText(context, "应用成功", Toast.LENGTH_SHORT).show();
        } catch (Exception localException) {
            localException.printStackTrace();
        }
//        wallpaperManager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_LOCK);
    }

    /**
     * 判断是否是使用我们的壁纸
     *
     * @param context
     * @return
     */
    public static boolean isUserWallpager(Context context) {
        WallpaperInfo wallpaperInfo = WallpaperManager.getInstance(context).getWallpaperInfo();
        if (wallpaperInfo == null) {
            return false;
        }

        String pkgName = wallpaperInfo.getPackageName();
        if (TextUtils.isEmpty(pkgName)) {
            return false;
        }

        String serviceName = wallpaperInfo.getServiceName();
        if (TextUtils.isEmpty(serviceName)) {
            return false;
        }

        if (pkgName.equals(context.getPackageName()) && serviceName.equals(SZWallpaperService.class.getCanonicalName())) {
            return true;
        }

        return false;
    }

    /**
     * 清楚壁纸
     *
     * @param context
     */
    public static void clearWallpaper(Context context) {
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
        try {
            wallpaperManager.clear();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 仅小米华为厂商支持
     *
     * @param context
     * @return
     */
    public static boolean isSupportWallpaper(Context context) {
        if (PhoneRomUtil.isVivo() || PhoneRomUtil.isHuaWei() || PhoneRomUtil.isXiaoMi() || PhoneRomUtil.isOneplus()) {
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                // 是否使用我们壁纸
                boolean isUsed = SZWallpaperUtils.isUserWallpager(context);
                if (isUsed) {
                    Log.w("snowzhao", "========>>>> 已经使用我们壁纸");
                    return false;
                }
                return true;
            }
        }
        return false;
    }
}
